﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;


namespace EHTSaleManager.Code.Utility
{
    public class HelpMethod
    {
        public static string MD5(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(input));

            StringBuilder s = new StringBuilder();
            foreach (byte p in hashedBytes)
            {
                s.Append(p.ToString("x").ToLower());
            }
            return s.ToString();
        }

        public static bool IsEmail(string input)
        {
            Regex RegexObj = new Regex(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$");
            return RegexObj.Match(input).Success;
        }

        public static bool IsUserID(string input)
        {
            Regex RegexObj = new Regex(@"[a-zA-Z_]+[a-zA-Z0-9_]");
            return RegexObj.Match(input).Success;
        }

        public static bool IsPassword(string input)
        {
            Regex RegexObj = new Regex(@"[a-zA-Z0-9_@$]");
            return RegexObj.Match(input).Success;
        }

        public static string ChangeDatetimeToString(DateTime dt)
        {
            string result;
            long tickDiff = DateTime.Now.Ticks - dt.Ticks;
            tickDiff = tickDiff / 10000000; //have seconds now
            int day = (int)(tickDiff / 86400);
            if (day == 0)
                result = dt.Hour + "h" + dt.Minute + "'|Hôm nay";
            else if (day == 1)
                result = dt.Hour + "h" + dt.Minute + "'|Hôm qua";
            else if (day == 2)
                result = dt.Hour + "h" + dt.Minute + "'|Hôm kia";
            else
                result = "cách đây " + dt.ToString() + " ngày";
            return result;
        }

        public static string CatString(string input, int max)
        {
            if (input.Length > max)
                return input.Substring(0, max) + "...";
            else
                return input;
        }

        public static string RemoveHtmlTag(string input)
        {
            input = input.Replace("&lt;", "<");
            input = input.Replace("&gt;", ">");
            input = input.Replace("&nbsp;", " ");

            char[] array = new char[input.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < input.Length; i++)
            {
                char let = input[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

        public static string FormatStringForUrl(String input)
        {
            String strNotSign = RemoveSign4VietnameseString(input);
            strNotSign = strNotSign.Replace(" ", "-");
            strNotSign = strNotSign.Replace("@", "");
            strNotSign = strNotSign.Replace("%", "");
            strNotSign = strNotSign.Replace("#", "");
            strNotSign = strNotSign.Replace("$", "");
            strNotSign = strNotSign.Replace("?", "");
            strNotSign = strNotSign.Replace("&", "");
            strNotSign = strNotSign.Replace(".", "");
            return strNotSign;
        }

        public static string RemoveSign4VietnameseString(string str)
        {
            string[] VietnameseSigns = new string[]
            {
                "aAeEoOuUiIdDyY",
                "áàạảãâấầậẩẫăắằặẳẵ",
                "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
                "éèẹẻẽêếềệểễ",
                "ÉÈẸẺẼÊẾỀỆỂỄ",
                "óòọỏõôốồộổỗơớờợởỡ",
                "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
                "úùụủũưứừựửữ",
                "ÚÙỤỦŨƯỨỪỰỬỮ",
                "íìịỉĩ",
                "ÍÌỊỈĨ",
                "đ",
                "Đ",
                "ýỳỵỷỹ",
                "ÝỲỴỶỸ"
            };

            //Tiến hành thay thế , lọc bỏ dấu cho chuỗi

            for (int i = 1; i < VietnameseSigns.Length; i++)
            {

                for (int j = 0; j < VietnameseSigns[i].Length; j++)

                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);

            }

            return str;
        }

        /// <summary>
        /// $(function () {
        //Google +1
        //$.getScript("http://apis.google.com/js/plusone.js", null, true);

        //Twitter
        //$.getScript("http://platform.twitter.com/widgets.js", null, true);

        //Facebook
        // $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1",  function(){

        //   $('body').append('<div id="fb-root"></div>'); 

        //   FB.init({ status: true, cookie: true, xfbml: true }); 

        //}, true);
        //});
        /// </summary>
        /// <param name="url"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static String CreateTwitter(String url, String title)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<a ");
            sb.Append("hreft='https://twitter.com/share' class='twitter-share-button' data-via='MY-TWITTER-HANDLE' data-count='horizontal' ");
            sb.Append("data-text = '" + title + "' ");
            sb.Append("data-url = '" + url + "' >");
            sb.Append("Tweet");
            sb.Append("</a>");

            return sb.ToString();
        }

        /// <summary>
        /// $(function () {
        //Google +1
        //$.getScript("http://apis.google.com/js/plusone.js", null, true);

        //Twitter
        //$.getScript("http://platform.twitter.com/widgets.js", null, true);

        //Facebook
        // $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1",  function(){

        //   $('body').append('<div id="fb-root"></div>'); 

        //   FB.init({ status: true, cookie: true, xfbml: true }); 

        //}, true);
        //});
        /// </summary>
        /// <param name="url"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static String CreateFaceBook(String url, String title)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div ");
            sb.Append("class='fb-like' data-send='false' data-layout='button_count' data-show-faces='false' data-font='arial' ");
            sb.Append("data-href = '" + url + "' >");
            sb.Append("</div>");

            return sb.ToString();
        }

        /// <summary>
        /// $(function () {
        //Google +1
        //$.getScript("http://apis.google.com/js/plusone.js", null, true);

        //Twitter
        //$.getScript("http://platform.twitter.com/widgets.js", null, true);

        //Facebook
        // $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1",  function(){

        //   $('body').append('<div id="fb-root"></div>'); 

        //   FB.init({ status: true, cookie: true, xfbml: true }); 

        //}, true);
        //});
        /// </summary>
        /// <param name="url"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static String CreateGooglePlusOne(String url, String title)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div ");
            sb.Append("class='g-plusone' data-size='medium' ");
            sb.Append("data-href = '" + url + "' >");
            sb.Append("</div>");

            return sb.ToString();
        }

        /// <summary>
        
        /// </summary>
        /// <param name="url"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static String CreateAllButtonShared(String url, String title)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul class='social'>");

            // google +
            sb.Append("<li class='social-google'>");
            sb.Append(CreateGooglePlusOne(url, title));
            sb.Append("</li>");

            // Twitter +
            sb.Append("<li class='social-twitter'>");
            sb.Append(CreateTwitter(url, title));
            sb.Append("</li>");

            // Facebook +
            sb.Append("<li class='social-facebook'>");
            sb.Append(CreateFaceBook(url, title));
            sb.Append("</li>");

            sb.Append("</ul>");

            return sb.ToString();
        }
    }
}
